package edu.cicese.alz;

import edu.cicese.alz.reports.AccelerometerUtil;
import org.jfree.ui.RefineryUtilities;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.LinkedList;

/**
 * Created by: Eduardo Quintana Contreras
 * Date: 24/11/11
 * Time: 03:04 PM
 */
public class SQLQuery {
	// Database
	public Connection connection;
	public Statement statement, statement2;
	public ResultSet resultSet1, resultSet2;
	public static int userID;

	public boolean openConnection() {
		try {
			// This will load the MySQL driver, each DB has its own driver
			Class.forName("com.mysql.jdbc.Driver");
			// Setup the connection with the DB
			connection = DriverManager.getConnection("jdbc:mysql://localhost/alzheimerdb", "root", "alz");
			// Statements allow to issue SQL queries to the database
			statement = connection.createStatement();
			statement2 = connection.createStatement();
			return true;
		} catch (ClassNotFoundException | SQLException e) {
			e.printStackTrace();
			return false;
		}
	}

	public void queryAccelerometer(int userID, String queryStr1, long type, int file) {
		try {
			openConnection();
			resultSet1 = statement.executeQuery(queryStr1);
			new FormatData().formatAccelerometer(userID, resultSet1, type, file);
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}

	public void queryAccelerometer(int userID, String queryStr1, int file) {
		try {
			openConnection();
			resultSet1 = statement.executeQuery(queryStr1);
//			new FormatData().formatAccelerometer(userID, resultSet1, file);
//			new FormatData().formatAccelerometerSimplified(userID, resultSet1, file);
			new FormatData().formatAccelerometerSimplified2(userID, resultSet1, file);
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}

	public void queryAccelerometerNFC(String queryStr1, String queryStr2, boolean summary, boolean saveToFile) {
		try {
			openConnection();
			resultSet1 = statement.executeQuery(queryStr1);
			resultSet2 = statement2.executeQuery(queryStr2);
			PlotData plot;
			plot = new PlotData("Plot", resultSet1, resultSet2, summary, saveToFile);
			plot.pack();
			RefineryUtilities.centerFrameOnScreen(plot);
			plot.setVisible(true);
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
//			close(connection, statement, resultSet1);
		}
	}

	public void queryNFC(String queryStr, boolean plotData, boolean createReport) {
		try {
			openConnection();
			resultSet1 = statement.executeQuery(queryStr);

			if (createReport) {
				ArrayList<ActivityMeasure> activities = new FormatData(resultSet1).formatNFC();
				if (plotData) {
					PlotData demo = new PlotData("Plot", activities);
					demo.pack();
					RefineryUtilities.centerFrameOnScreen(demo);
					demo.setVisible(true);
				}
			}

			else if (plotData) {
				PlotData demo = new PlotData("Plot", resultSet1);
				demo.pack();
				RefineryUtilities.centerFrameOnScreen(demo);
				demo.setVisible(true);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}

	public void queryAccelerometerNFCSummary(String queryStr1, String queryStr2) {
		try {
			openConnection();
			resultSet1 = statement.executeQuery(queryStr1);
			resultSet2 = statement2.executeQuery(queryStr2);
			PlotData demo = new PlotData("Plot", resultSet1, resultSet2);
			demo.pack();
			RefineryUtilities.centerFrameOnScreen(demo);
			demo.setVisible(true);
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
//			close(connection, statement, resultSet1);
		}
	}

	public ResultSet query(String queryStr) {
		try {
			return statement.executeQuery(queryStr);
		} catch (SQLException e) {
			e.printStackTrace();
			return null;
		} finally {
//			close(connection, statement, resultSet1);
//			return null;
		}
	}

	//TODO: ORDER BY?

	private String accelerometerPeriodQuery(int userID, long timestamp1, long timestamp2) {
		String queryStr = "";
		LinkedList<Integer> accelerometerTables = getAccelerometerTables(userID, timestamp1, timestamp2);
		if (/*accelerometerTables != null && */accelerometerTables.size() > 0) {
			if (accelerometerTables.size() > 2) {
				System.out.println("accelerometerTables.size() = " + accelerometerTables.size() + ". Why? Repeated " +
						"timestamps? Wrong infiles? What?!");
			}
			else {
				int firstTable = accelerometerTables.getFirst();
				int lastTable = accelerometerTables.getLast();
				System.out.println(firstTable + " " + lastTable);
				for (int tID = firstTable; tID <= lastTable; tID++) {
					queryStr += "SELECT AXIS_X, AXIS_Y, AXIS_Z, TIMESTAMP, USER_ID\n" +
							"FROM accelerometer_" + userID + "_" + tID + "\n";
					if (tID == firstTable || tID == lastTable) {
						queryStr += "WHERE TIMESTAMP >= " + timestamp1 + " AND TIMESTAMP <= " + timestamp2 + "\n";
					}
					if (tID != lastTable) {
						queryStr += "UNION\n";
					}
				}
			}
		}
		else {
			System.out.println("accelerometerTables = EMPTY. Timestamp not in records?");
		}
		return queryStr;
	}

	private LinkedList<Integer> getAccelerometerTables(int userID, long timestamp1, long timestamp2) {
		LinkedList<Integer> accelerometerTables = new LinkedList<>();
		String queryStr;
		int tables = 0;
		try {
			resultSet1 = statement.executeQuery(new DataCapture().selectUserTablesQuery(userID));
			if (resultSet1.next()) {
				tables = resultSet1.getInt(1);
			}

			if (tables > 0) {
				long first = 0, last = 0;
				resultSet1 = statement.executeQuery("SELECT FIRST FROM table_limit WHERE USER_ID = " + userID + " AND TABLE_ID = 1");
				if (resultSet1.next()) {
					first = resultSet1.getLong(1);
				}

				if (timestamp1 < first) {
					accelerometerTables.add(1);
				}
				else {
					queryStr = "SELECT TABLE_ID\n" +
							"FROM table_limit\n" +
							"WHERE USER_ID = " + userID + " AND FIRST <= " + timestamp1 + " AND LAST >= " + timestamp1;
					resultSet1 = statement.executeQuery(queryStr);
					if (resultSet1.next()) {
						accelerometerTables.add(resultSet1.getInt(1));
					}
				}

				resultSet1 = statement.executeQuery("SELECT LAST FROM table_limit WHERE USER_ID = " + userID + " AND TABLE_ID = " + tables);
				if (resultSet1.next()) {
					last = resultSet1.getLong(1);
				}

				if (timestamp2 > last) {
					accelerometerTables.add(tables);
				}
				else {
					queryStr = "SELECT TABLE_ID\n" +
							"FROM table_limit\n" +
							"WHERE USER_ID = " + userID + " AND FIRST <= " + timestamp2 + " AND LAST >= " + timestamp2;
					resultSet1 = statement.executeQuery(queryStr);
					if (resultSet1.next()) {
						accelerometerTables.add(resultSet1.getInt(1));
					}
				}


			}
//			else {
//				return accelerometerTables;
//			}
		} catch (SQLException e) {
			e.printStackTrace();
		}

		return accelerometerTables;
	}
	
	private String nfcPeriodQuery(int userID, long timestamp1, long timestamp2) {
		return "SELECT t1.MESSAGE, t1.MESSAGE_ID, t1.TIMESTAMP\n" +
				"FROM nfc AS t1\n" +
				"WHERE t1.USER_ID = " + userID + " AND\n" +
				"t1.TIMESTAMP >= " + timestamp1 + " AND t1.TIMESTAMP <= " + timestamp2;
	}

	private String gpsPeriodQuery(int userID, long timestamp1, long timestamp2) {
		return "SELECT t1.USER_ID, t1.LATITUDE, t1.LONGITUDE, t1.TIMESTAMP, t1.ALTITUDE, t1.ACCURACY\n" +
				"FROM gps AS t1\n" +
				"WHERE t1.USER_ID = " + userID + " AND\n" +
				"t1.TIMESTAMP >= " + timestamp1 + " AND t1.TIMESTAMP <= " + timestamp2;
	}

	private String wifiPeriodQuery(int userID, long timestamp1, long timestamp2) {
		return "SELECT t1.USER_ID, t1.BSSID, t1.LEVEL, t1.SSID, t1.TIMESTAMP\n" +
				"FROM wifi AS t1\n" +
				"WHERE t1.USER_ID = " + userID + " AND\n" +
				"t1.TIMESTAMP >= " + timestamp1 + " AND t1.TIMESTAMP <= " + timestamp2;
	}




	private void truncateAccelerometerTables(int userID) {
		openConnection();
		String queryStr = "SELECT TABLES\n" +
				"FROM participant\n" +
				"WHERE USER_ID = " + userID;
		try {
			resultSet1 = statement.executeQuery(queryStr);
			int tables = 0;
			while (resultSet1.next()) {
				tables = resultSet1.getInt(1);
			}
			for (int i = 1; i <= tables; i++) {
				queryStr = "TRUNCATE TABLE accelerometer_" + userID + "_" + i + ";\n";
				statement.executeUpdate(queryStr);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}

	private void dropAccelerometerTables(int userID) {
		openConnection();
		String queryStr = "SELECT TABLES\n" +
				"FROM participant\n" +
				"WHERE USER_ID = " + userID;
		try {
			resultSet1 = statement.executeQuery(queryStr);
			int tables = 0;
			while (resultSet1.next()) {
				tables = resultSet1.getInt(1);
			}
			for (int i = 1; i <= tables; i++) {
				queryStr = "DROP TABLE accelerometer_" + userID + "_" + i + ";\n";
				statement.executeUpdate(queryStr);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}

	private void truncateTableLimitTables(int userID) {
		openConnection();
		String queryStr = "DELETE FROM table_limit WHERE USER_ID = " + userID;
		try {
			statement.executeUpdate(queryStr);
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}


	private void plotGPSData(int userID) {
		plotGPSData(userID, DataUtil.dates[userID - 1][0], DataUtil.dates[userID - 1][1], true);
	}

	private void plotGPSData(int userID, String day) {
		plotGPSData(userID, day, day, false);
	}

	private void plotGPSData(int userID, String begin, String end, boolean complete) {
		// DATE INPUT FORMAT
		//("dd/MM/yyyy HH:mm:ss")
		long beginDate = TimeUtil.getDateInMs(begin + " 00:00:00");
		long endDate = TimeUtil.getDateInMs(end + " 24:00:00");

		System.out.println(beginDate + "  " + endDate);

		openConnection();
		String queryStr = gpsPeriodQuery(userID, beginDate, endDate);
		ResultSet resultSet = query(queryStr);

		GPSChart gpsChart = new GPSChart(resultSet, userID, complete);
		gpsChart.pack();
		RefineryUtilities.centerFrameOnScreen(gpsChart);
		gpsChart.setVisible(true);
	}


	private void plotAccelerometerSummaryData(int userID) {
		long beginDate = DataUtil.tsDuration[userID - 1][0];
		long endDate = DataUtil.tsDuration[userID - 1][1];
		plotAccelerometerSummaryData(userID, beginDate, endDate);
	}

	private long plotAccelerometerSummaryData(int userID, String day, int part) {
		long beginDate, endDate;
		if (part == 1) {
			beginDate = TimeUtil.getDateInMs(day + " 00:00:00");
			endDate = TimeUtil.getDateInMs(day + " 12:00:00");
		}
		else {
			beginDate = TimeUtil.getDateInMs(day + " 12:00:00");
			endDate = TimeUtil.getDateInMs(day + " 24:00:00");
		}
		return plotAccelerometerSummaryData(userID, beginDate, endDate);
	}

	private long plotAccelerometerSummaryData(int userID, long beginDate, long endDate) {
		openConnection();
		try {
			String queryStr = accelerometerPeriodQuery(userID, beginDate, endDate);
			ResultSet resultSet = query(queryStr);

			AccelerometerChart accelerometerChart = new AccelerometerChart(resultSet, userID, beginDate, endDate);
			accelerometerChart.pack();
			RefineryUtilities.centerFrameOnScreen(accelerometerChart);
			accelerometerChart.setVisible(true);
			return accelerometerChart.filename;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return 0;
	}

	private void plotAccelerometerSummaryDataFromFiles(int userID, String path) {
		openConnection();
		try {
			AccelerometerChart accelerometerChart = new AccelerometerChart(userID, path);
			accelerometerChart.pack();
			RefineryUtilities.centerFrameOnScreen(accelerometerChart);
			accelerometerChart.setVisible(true);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}


	private void plotAccelerometerIntensity(int userID) {
		long beginDate = DataUtil.tsDuration[userID - 1][0];
		long endDate = DataUtil.tsDuration[userID - 1][1];
		plotAccelerometerIntensity(userID, beginDate, endDate);
	}

	private long plotAccelerometerIntensity(int userID, String day, int part) {
		long beginDate, endDate;
		if (part == 1) {
			beginDate = TimeUtil.getDateInMs(day + " 00:00:00");
			endDate = TimeUtil.getDateInMs(day + " 12:00:00");
		}
		else {
			beginDate = TimeUtil.getDateInMs(day + " 12:00:00");
			endDate = TimeUtil.getDateInMs(day + " 24:00:00");
		}
		return plotAccelerometerIntensity(userID, beginDate, endDate);
	}

	private long plotAccelerometerIntensity(int userID, long beginDate, long endDate) {
		openConnection();
		try {
			String queryStr = accelerometerPeriodQuery(userID, beginDate, endDate);
			ResultSet resultSet = query(queryStr);

			AccelerometerChart accelerometerChart = new AccelerometerChart(resultSet, userID, beginDate, endDate, true);
//			accelerometerChart.pack();
//			RefineryUtilities.centerFrameOnScreen(accelerometerChart);
//			accelerometerChart.setVisible(true);
			return accelerometerChart.filename;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return 0;
		//initialize timestampAux
	}

	private void plotAccelerometerIntensityFromFiles(int userID, String path) {
		AccelerometerChart accelerometerChart = new AccelerometerChart(userID, path, true);
		accelerometerChart.pack();
		RefineryUtilities.centerFrameOnScreen(accelerometerChart);
		accelerometerChart.setVisible(true);
	}


	private void plotAccelerometerAndNFCSummaryData(int userID, String day) {
		// DATE INPUT FORMAT
		//("dd/MM/yyyy HH:mm:ss")
		long beginDate = TimeUtil.getDateInMs(day + " 00:00:00");
		long endDate = TimeUtil.getDateInMs(day + " 24:00:00");

		openConnection();
		String queryStr1 = accelerometerPeriodQuery(userID, beginDate, endDate);
		String queryStr2 = nfcPeriodQuery(userID, beginDate, endDate);
		queryAccelerometerNFC(queryStr1, queryStr2, true, false);
	}

	private void plotAccelerometerAndNFCData(int userID, String day) {
		// DATE INPUT FORMAT
		//("dd/MM/yyyy HH:mm:ss")
		long beginDate = TimeUtil.getDateInMs(day + " 00:00:00");
		long endDate = TimeUtil.getDateInMs(day + " 24:00:00");

		plotAccelerometerAndNFCData(userID, beginDate, endDate);
	}

	private void plotAccelerometerAndNFCData(int userID, String day, String begin, String end) {
		// DATE INPUT FORMAT
		//("dd/MM/yyyy HH:mm:ss")
		long beginDate = TimeUtil.getDateInMs(day + " " + begin);
		long endDate = TimeUtil.getDateInMs(day + " " + end);

		plotAccelerometerAndNFCData(userID, beginDate, endDate);
	}

	private void plotAccelerometerAndNFCData(int userID, long beginDate, long endDate) {
		openConnection();
		String queryStr1 = accelerometerPeriodQuery(userID, beginDate, endDate);
		String queryStr2 = nfcPeriodQuery(userID, beginDate, endDate);
		queryAccelerometerNFC(queryStr1, queryStr2, false, true);
	}


	private void plotNFCSummaryData(int userID) {
		String beginDay = DataUtil.dates[userID - 1][0];
		String endDay = DataUtil.dates[userID - 1][1];
		long beginDate = TimeUtil.getDateInMs(beginDay + " 00:00:00");
		long endDate = TimeUtil.getDateInMs(endDay + " 24:00:00") - 1;
		plotNFCSummaryData(userID, beginDate, endDate);
	}

	private void plotNFCSummaryData(int userID, String day) {
		long beginDate = TimeUtil.getDateInMs(day + " 00:00:00");
		long endDate = TimeUtil.getDateInMs(day + " 24:00:00") - 1;
		plotNFCSummaryData(userID, beginDate, endDate);
	}

	private void plotNFCSummaryData(int userID, long beginDate, long endDate) {
		openConnection();
		try {
			String queryStr = nfcPeriodQuery(userID, beginDate, endDate);
			ResultSet resultSet = query(queryStr);

			NFCChart nfcChart = new NFCChart(resultSet, userID, beginDate, endDate);
			nfcChart.pack();
			RefineryUtilities.centerFrameOnScreen(nfcChart);
			nfcChart.setVisible(true);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	private void plotNFCSummaryDataFromFiles(int userID) {
		openConnection();
		try {
			NFCChart nfcChart = new NFCChart(userID);
			nfcChart.pack();
			RefineryUtilities.centerFrameOnScreen(nfcChart);
			nfcChart.setVisible(true);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	private void plotNFCData(int userID, String day, boolean plotData, boolean createReport) {
		// DATE INPUT FORMAT
		//("dd/MM/yyyy HH:mm:ss")
		long beginDate = TimeUtil.getDateInMs(day + " 00:00:00");
		long endDate = TimeUtil.getDateInMs(day + " 24:00:00");
//		long beginDate = TimeUtil.getDateInMs("1/10/2011 00:00:00");
//		long endDate = TimeUtil.getDateInMs("1/03/2012 24:00:00");

		openConnection();
		String queryStr = nfcPeriodQuery(userID, beginDate, endDate);
		queryNFC(queryStr, plotData, createReport);
	}




	private void saveAccelerometerData() {
		openConnection();
		for (int userID = 1; userID <= 15; userID++) {
			String[][] userData = AccelerometerUtil.walkingPeriods[userID - 1];
			int file = 1;
			for (String[] userWalkingPeriods : userData) {
				long beginDate = TimeUtil.getDateInMs(userWalkingPeriods[0] + " " + userWalkingPeriods[1]);
				long endDate = TimeUtil.getDateInMs(userWalkingPeriods[0] + " " + userWalkingPeriods[2]);
				queryAccelerometer(userID, accelerometerPeriodQuery(userID, beginDate, endDate), file);
				file++;
			}
		}



//		int userID = 1, c;
//		for (long[][] user : AccelerometerUtil.timestamps) {
//			c = 1;
//			for (long[] data : user) {
//				openConnection();
//				queryAccelerometer(userID, accelerometerPeriodQuery(userID, data[1]/* - 86400000*/, data[2]/* - 86400000*/), data[0], c);
//				c++;
//			}
//			userID++;
//		}

	}


	/*public void displayGPSData(int userID) {
		displayGPSData(userID, DataUtil.dates[userID - 1][0], DataUtil.dates[userID - 1][0], false, false);
	}

	public void displayGPSData(int userID, boolean clusterLocations, boolean route) {
		displayGPSData(userID, DataUtil.dates[userID - 1][0], DataUtil.dates[userID - 1][1], clusterLocations, route);
	}

	public void displayGPSData(int userID, String day, boolean clusterLocations, boolean route) {
		displayGPSData(userID, day, day, clusterLocations, route);
	}

	public void displayGPSData(int userID, String begin, String end, boolean clusterLocations, boolean route) {
		// DATE INPUT FORMAT
		//("dd/MM/yyyy HH:mm:ss")
		long beginDate = TimeUtil.getDateInMs(begin + " 00:00:00");
		long endDate = TimeUtil.getDateInMs(end + " 24:00:00");

		openConnection();
		String queryStr = gpsPeriodQuery(userID, beginDate, endDate);
		ResultSet resultSet = query(queryStr);

		FormatData formatData = new FormatData(resultSet);
		// Format GPS data and save it to a local file
//		formatData.formatGPS();
		// Format GPS data for Map Viewer
//		MapDataOld mapData = formatData.formatForMapViewer();
		MapDataOld mapData = formatData.formatForMapViewerB();
		if (clusterLocations) {
			ArrayList<MapLocationOld> mapLocations = (ArrayList) mapData.getMapLocations();
			mapLocations = clusterLocations(mapLocations);
			List<URoute> uRoutes;
			if (!route) {
				uRoutes = new ArrayList<>();
			}
			else {
				uRoutes = RouteUtils.findKnownRoutes(
						mapData.getMapMarkers(), mapData.getMapLocations(), mapData.getMapLines());
			}
			new Map(userID, begin, mapData.getMapMarkers(), mapData.getMapLines(), mapData.getMapLabels(), mapLocations,
					uRoutes, false, false, false, true, false);

//			RouteUtils.findKnownRoutes(mapData.getMapMarkers(), mapData.getMapLocations());
		}
		else {
			List<URoute> uRoutes;
			if (!route) {
				uRoutes = new ArrayList<>();
			}
			else {
				uRoutes = RouteUtils.findKnownRoutes(
						mapData.getMapMarkers(), mapData.getMapLocations(), mapData.getMapLines());
			}
			new Map(userID, begin, mapData.getMapMarkers(), mapData.getMapLines(),
					mapData.getMapLabels(), mapData.getMapLocations(), uRoutes);
		}
	}

	public void createGPSClusterReport(int userID, String begin, String end) {
		// DATE INPUT FORMAT
		//("dd/MM/yyyy HH:mm:ss")
		long beginDate = TimeUtil.getDateInMs(begin + " 00:00:00");
		long endDate = TimeUtil.getDateInMs(end + " 24:00:00");

		openConnection();
		String queryStr = gpsPeriodQuery(userID, beginDate, endDate);
		ResultSet resultSet = query(queryStr);

		FormatData formatData = new FormatData(resultSet);
		MapDataOld mapData = formatData.formatForMapViewerB();
		ArrayList<MapLocationOld> mapLocations = (ArrayList) mapData.getMapLocations();
		mapLocations = clusterLocations(mapLocations);
		System.out.println(mapLocations.size());
	}*/

	/*private ArrayList<MapLocationOld> clusterLocations(ArrayList<MapLocationOld> mapLocations) {
		// Group locations
		ArrayList<LocationCluster> locationClusters = new ArrayList<>();
		for (MapLocationOld l1 : mapLocations) {
			LocationCluster locationCluster = new LocationCluster();
			locationCluster.add(l1);
			for (MapLocationOld l2 : mapLocations) {
				if (l1 != l2) {
					if (GPSUtilities.getSLCDistanceFrom(l1.getLat(), l1.getLon(), l2.getLat(), l2.getLon()) <= 50) {
						locationCluster.add(l2);
					}
				}
			}
			locationClusters.add(locationCluster);
		}
		Collections.sort(locationClusters);

		// Remove groups duplicated
		ArrayList<LocationCluster> finalLocationClusters = new ArrayList<>();
		for (LocationCluster lc1 : locationClusters) {
			if (!lc1.getLocations().isEmpty()) {
				LocationCluster newLocation = new LocationCluster();
				newLocation.getLocations().addAll(lc1.getLocations());
				for (LocationCluster lc2 : locationClusters) {
					if (lc1 != lc2) {
						if (lc2.getLocations().removeAll(newLocation.getLocations())) {
							newLocation.getLocations().addAll(lc2.getLocations());
						}
					}
				}
				finalLocationClusters.add(newLocation);
			}
		}

		// Obtain the average center of the location
		ArrayList<MapLocationOld> finalMapLocations = new ArrayList<>();
		for (LocationCluster locationCluster : finalLocationClusters) {
			double[] maxMin = locationCluster.getMaxMin();
			locationCluster.calculateTime();
			MapLocationOld mapLocation = new MapLocationOld(maxMin[0], maxMin[1], maxMin[2], maxMin[3], maxMin[4],
					maxMin[5], locationCluster.getLocations().size(), locationCluster.getLocations().size() + "",  locationCluster.getTime());
			finalMapLocations.add(mapLocation);
		}
//		System.out.println(finalMapLocations.size() + " clusters found.");

		return finalMapLocations;
	}*/

	private void createWifiDataFile(int userID, String day) {
		// DATE INPUT FORMAT
		//("dd/MM/yyyy HH:mm:ss")
		long beginDate = TimeUtil.getDateInMs(day + " 00:00:00");
		long endDate = TimeUtil.getDateInMs(day + " 24:00:00");


		openConnection();
		String queryStr = wifiPeriodQuery(userID, beginDate, endDate);
		ResultSet resultSet = query(queryStr);

		FormatData formatData = new FormatData(resultSet);
		formatData.formatWifi(resultSet);
	}

	public static void main(String[] args) {
		//("dd/MM/yyyy HH:mm:ss")
		String day = "16/4/2012";
		userID = 15;
		long d1, d2;

		String begin = "00:00:00";
		String end = "24:00:00";

		String beginDay = "30/04/2012";
		String endDay = "30/05/2012";

		long beginDate = TimeUtil.getDateInMs(beginDay + " 00:00:00");
		long endDate = TimeUtil.getDateInMs(endDay + " 24:00:00") - 1;

//		new SQLQuery().plotNFCSummaryData(104, beginDate, endDate);

//		new SQLQuery().plotAccelerometerAndNFCData(userID, day, begin, end);
//		new SQLQuery().plotAccelerometerAndNFCData(userID, day);
		float alpha = 0.8f;
//		AccelerometerCount.initiateGravity();
//		AccelerometerCount.setAlpha(alpha);

		/*for (userID = 103; userID <= 104; userID++) {
			AccelerometerCount.initiateGravity();
			AccelerometerCount.setAlpha(alpha);
			for (int i = DataUtil.daysRene[userID - 101][0]; i <= 30; i++) {
				d1 = new SQLQuery().plotAccelerometerIntensity(userID, i + "/04/2012", 1);
				d2 = new SQLQuery().plotAccelerometerIntensity(userID, i + "/04/2012", 2);
				AccelerometerChart.joinFiles("D:\\_proyectoAlz\\analysis\\accelerometer\\counts\\" + alpha + "\\" + userID + "\\", d1 + ".alz", d1 + ".alz5", d2 + ".alz5");
				AccelerometerChart.joinFiles("D:\\_proyectoAlz\\analysis\\accelerometer\\counts\\" + alpha + "\\" + userID + "\\", d1 + ".ct", d1 + ".ct5", d2 + ".ct5");
			}
			for (int i = 1; i <= DataUtil.daysRene[userID - 101][1]; i++) {
				d1 = new SQLQuery().plotAccelerometerIntensity(userID, i + "/05/2012", 1);
				d2 = new SQLQuery().plotAccelerometerIntensity(userID, i + "/05/2012", 2);
				AccelerometerChart.joinFiles("D:\\_proyectoAlz\\analysis\\accelerometer\\counts\\" + alpha + "\\" + userID + "\\", d1 + ".alz", d1 + ".alz5", d2 + ".alz5");
				AccelerometerChart.joinFiles("D:\\_proyectoAlz\\analysis\\accelerometer\\counts\\" + alpha + "\\" + userID + "\\", d1 + ".ct", d1 + ".ct5", d2 + ".ct5");
			}

//			new SQLQuery().plotAccelerometerIntensityFromFiles(userID, "D:\\_proyectoAlz\\analysis\\accelerometer\\counts\\" + alpha + "\\" + userID + "\\");
		}*/

//		new SQLQuery().plotAccelerometerIntensityFromFiles(103, "D:\\_proyectoAlz\\analysis\\accelerometer\\counts\\" + alpha + "\\" + 103 + "\\");
//		new SQLQuery().plotAccelerometerIntensityFromFiles(103, "D:\\_proyectoAlz\\analysis\\accelerometer\\counts\\" + alpha + "\\" + 103 + "\\");
		

//		for (int user = 1; user < 16; user++) {
//			new SQLQuery().displayGPSData(user, true, false);
//		}
//		new SQLQuery().displayGPSData(2, true, false);

//		new SQLQuery().plotGPSData(10);
//		new SQLQuery().saveAccelerometerData();
//		ExcelUtil.getAverages("D:\\_proyectoAlz\\analysis\\accelerometer\\walking\\");
//		ExcelUtil.joinUsers("D:\\_proyectoAlz\\analysis\\accelerometer\\walking\\summary\\");

//		new SQLQuery().plotGPSData(3);

//		AccelerometerCount.getActivityCountsPerDay("D:\\_proyectoAlz\\analysis\\accelerometer\\counts\\0.5\\" + userID + "\\");
//		AccelerometerCount.getActivityCountsPerHour("D:\\_proyectoAlz\\analysis\\accelerometer\\counts\\0.5\\" + userID + "\\");

		//float[] alphas = new float[]{0.5f, 0.6f, 0.7f, 0.8f, 0.9f};

		/*userID = 4;
		float[] alphas = new float[]{0.9f};
		for (int a = 0; a < alphas.length; a++) {
			AccelerometerCount.initiateGravity();
			AccelerometerCount.setAlpha(alphas[a]);
			try {
				for (int i = DataUtil.days1_5[userID - 1][0]; i <= 30; i++) {
					d1 = new SQLQuery().plotAccelerometerIntensity(userID, i + "/11/2011", 1);
					d2 = new SQLQuery().plotAccelerometerIntensity(userID, i + "/11/2011", 2);
					AccelerometerChart.joinFiles("D:\\_proyectoAlz\\analysis\\accelerometer\\counts\\" + alphas[a] + "\\" + userID + "\\", d1 + ".alz", d1 + ".alz5", d2 + ".alz5");
					AccelerometerChart.joinFiles("D:\\_proyectoAlz\\analysis\\accelerometer\\counts\\" + alphas[a] + "\\" + userID + "\\", d1 + ".ct", d1 + ".ct5", d2 + ".ct5");
				}
				for (int i = 1; i <= DataUtil.days1_5[userID - 1][1]; i++) {
					d1 = new SQLQuery().plotAccelerometerIntensity(userID, i + "/12/2011", 1);
					d2 = new SQLQuery().plotAccelerometerIntensity(userID, i + "/12/2011", 2);
					AccelerometerChart.joinFiles("D:\\_proyectoAlz\\analysis\\accelerometer\\counts\\" + alphas[a] + "\\" + userID + "\\", d1 + ".alz", d1 + ".alz5", d2 + ".alz5");
					AccelerometerChart.joinFiles("D:\\_proyectoAlz\\analysis\\accelerometer\\counts\\" + alphas[a] + "\\" + userID + "\\", d1 + ".ct", d1 + ".ct5", d2 + ".ct5");
				}

				new SQLQuery().plotAccelerometerIntensityFromFiles(userID, "D:\\_proyectoAlz\\analysis\\accelerometer\\counts\\" + alphas[a] + "\\" + userID + "\\");

			} catch (Exception e) {
				e.printStackTrace();
			}
		}*/

		/*float[] alphas = new float[]{0.5f, 0.6f, 0.7f, 0.8f, 0.9f};
		for (int a = 0; a < alphas.length; a++) {
			System.out.println("Alpha " + alphas[a]);
			for (userID = 1; userID <= 15; userID++) {
				new SQLQuery().plotAccelerometerIntensityFromFiles(userID, "D:\\_proyectoAlz\\analysis\\accelerometer\\counts\\" + alphas[a] + "\\" + userID + "\\");
			}
		}*/

//		alpha = 0.8f;
//		for (userID = 1; userID <= 15; userID++) {
//			new SQLQuery().plotAccelerometerIntensityFromFiles(userID, "D:\\_proyectoAlz\\analysis\\accelerometer\\counts\\" + alpha + "\\" + userID + "\\");
//		}


		//////////////////////////////////////////////////////////////////////////////////////////////
		///           ACTIVITY INTENSITY
		//////////////////////////////////////////////////////////////////////////////////////////////


		/*userID = 5;
		for (int a = 0; a < alphas.length; a++) {
			AccelerometerCount.initiateGravity();
			AccelerometerCount.setAlpha(alphas[a]);
			try {
				for (int i = DataUtil.days1_5[userID - 1][0]; i <= 30; i++) {
					d1 = new SQLQuery().plotAccelerometerIntensity(userID, i + "/11/2011", 1);
					d2 = new SQLQuery().plotAccelerometerIntensity(userID, i + "/11/2011", 2);
					AccelerometerChart.joinFiles("D:\\_proyectoAlz\\analysis\\accelerometer\\counts\\" + alphas[a] + "\\" + userID + "\\", d1 + ".alz", d1 + ".alz5", d2 + ".alz5");
					AccelerometerChart.joinFiles("D:\\_proyectoAlz\\analysis\\accelerometer\\counts\\" + alphas[a] + "\\" + userID + "\\", d1 + ".ct", d1 + ".ct5", d2 + ".ct5");
				}
				for (int i = 1; i <= DataUtil.days1_5[userID - 1][1]; i++) {
					d1 = new SQLQuery().plotAccelerometerIntensity(userID, i + "/12/2011", 1);
					d2 = new SQLQuery().plotAccelerometerIntensity(userID, i + "/12/2011", 2);
					AccelerometerChart.joinFiles("D:\\_proyectoAlz\\analysis\\accelerometer\\counts\\" + alphas[a] + "\\" + userID + "\\", d1 + ".alz", d1 + ".alz5", d2 + ".alz5");
					AccelerometerChart.joinFiles("D:\\_proyectoAlz\\analysis\\accelerometer\\counts\\" + alphas[a] + "\\" + userID + "\\", d1 + ".ct", d1 + ".ct5", d2 + ".ct5");
				}

				new SQLQuery().plotAccelerometerIntensityFromFiles(userID, "D:\\_proyectoAlz\\analysis\\accelerometer\\counts\\" + alphas[a] + "\\" + userID + "\\");

			} catch (Exception e) {
				e.printStackTrace();
			}
		}*/


		/*try {
			userID = 6;
			for (int a = 0; a < alphas.length; a++) {
				AccelerometerCount.initiateGravity();
				AccelerometerCount.setAlpha(alphas[a]);
				for (int i = 8; i <= 31; i++) {
					d1 = new SQLQuery().plotAccelerometerIntensity(userID, i + "/12/2011", 1);
					d2 = new SQLQuery().plotAccelerometerIntensity(userID, i + "/12/2011", 2);
					AccelerometerChart.joinFiles("D:\\_proyectoAlz\\analysis\\accelerometer\\counts\\" + alphas[a] + "\\" + userID + "\\", d1 + ".alz", d1 + ".alz5", d2 + ".alz5");
					AccelerometerChart.joinFiles("D:\\_proyectoAlz\\analysis\\accelerometer\\counts\\" + alphas[a] + "\\" + userID + "\\", d1 + ".ct", d1 + ".ct5", d2 + ".ct5");
				}

				for (int i = 1; i <= 3; i++) {
					d1 = new SQLQuery().plotAccelerometerIntensity(userID, i + "/1/2012", 1);
					d2 = new SQLQuery().plotAccelerometerIntensity(userID, i + "/1/2012", 2);
					AccelerometerChart.joinFiles("D:\\_proyectoAlz\\analysis\\accelerometer\\counts\\" + alphas[a] + "\\" + userID + "\\", d1 + ".alz", d1 + ".alz5", d2 + ".alz5");
					AccelerometerChart.joinFiles("D:\\_proyectoAlz\\analysis\\accelerometer\\counts\\" + alphas[a] + "\\" + userID + "\\", d1 + ".ct", d1 + ".ct5", d2 + ".ct5");
				}

				new SQLQuery().plotAccelerometerIntensityFromFiles(userID, "D:\\_proyectoAlz\\analysis\\accelerometer\\counts\\" + alphas[a] + "\\" + userID + "\\");
			}

		} catch (Exception e) {
			e.printStackTrace();
		}*/

/*		try {
//			for (userID = 7; userID <= 10; userID++) {
				userID = 10;
				for (int a = 0; a < alphas.length; a++) {
					AccelerometerCount.initiateGravity();
					AccelerometerCount.setAlpha(alphas[a]);
					for (int i = DataUtil.days7_10[userID - 7][0]; i <= DataUtil.days7_10[userID - 7][1]; i++) {
						d1 = new SQLQuery().plotAccelerometerIntensity(userID, i + "/12/2011", 1);
						d2 = new SQLQuery().plotAccelerometerIntensity(userID, i + "/12/2011", 2);
						AccelerometerChart.joinFiles("D:\\_proyectoAlz\\analysis\\accelerometer\\counts\\" + alphas[a] + "\\" + userID + "\\", d1 + ".alz", d1 + ".alz5", d2 + ".alz5");
						AccelerometerChart.joinFiles("D:\\_proyectoAlz\\analysis\\accelerometer\\counts\\" + alphas[a] + "\\" + userID + "\\", d1 + ".ct", d1 + ".ct5", d2 + ".ct5");
					}

//					new SQLQuery().plotAccelerometerIntensityFromFiles(userID, "D:\\_proyectoAlz\\analysis\\accelerometer\\counts\\" + alphas[a] + "\\" + userID + "\\");
				}
//			}

		} catch (Exception e) {
			e.printStackTrace();
		}

		try {
			for (userID = 11; userID <= 15; userID++) {
				for (int a = 0; a < alphas.length; a++) {
					AccelerometerCount.initiateGravity();
					AccelerometerCount.setAlpha(alphas[a]);
					for (int i = DataUtil.daysLast5[userID - 11][0]; i <= 31; i++) {
						d1 = new SQLQuery().plotAccelerometerIntensity(userID, i + "/1/2012", 1);
						d2 = new SQLQuery().plotAccelerometerIntensity(userID, i + "/1/2012", 2);
						AccelerometerChart.joinFiles("D:\\_proyectoAlz\\analysis\\accelerometer\\counts\\" + alphas[a] + "\\" + userID + "\\", d1 + ".alz", d1 + ".alz5", d2 + ".alz5");
						AccelerometerChart.joinFiles("D:\\_proyectoAlz\\analysis\\accelerometer\\counts\\" + alphas[a] + "\\" + userID + "\\", d1 + ".ct", d1 + ".ct5", d2 + ".ct5");
					}
					for (int i = 1; i <= DataUtil.daysLast5[userID - 11][1]; i++) {
						d1 = new SQLQuery().plotAccelerometerIntensity(userID, i + "/2/2012", 1);
						d2 = new SQLQuery().plotAccelerometerIntensity(userID, i + "/2/2012", 2);
						AccelerometerChart.joinFiles("D:\\_proyectoAlz\\analysis\\accelerometer\\counts\\" + alphas[a] + "\\" + userID + "\\", d1 + ".alz", d1 + ".alz5", d2 + ".alz5");
						AccelerometerChart.joinFiles("D:\\_proyectoAlz\\analysis\\accelerometer\\counts\\" + alphas[a] + "\\" + userID + "\\", d1 + ".ct", d1 + ".ct5", d2 + ".ct5");
					}
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}*/



//		AccelerometerCount.initiateGravity();
//		AccelerometerCount.setAlpha(0.8f);
/*		try {
			for (userID = 1; userID <= 5; userID++) {
//  			userID = 5;
//				for (int i = 15; i <= 30; i++) {
//					d1 = new SQLQuery().plotAccelerometerIntensity(userID, i + "/11/2011", 1);
//					d2 = new SQLQuery().plotAccelerometerIntensity(userID, i + "/11/2011", 2);
//					System.out.println(d1 + " " + d2);
//					AccelerometerChart.joinFiles("D:\\_proyectoAlz\\analysis\\accelerometer\\counts\\" + userID + "\\", d1 + ".alz", d1 + ".alz5", d2 + ".alz5");
//				}
//
//				for (int i = 1; i <= 5; i++) {
//					d1 = new SQLQuery().plotAccelerometerIntensity(userID, i + "/12/2011", 1);
//					d2 = new SQLQuery().plotAccelerometerIntensity(userID, i + "/12/2011", 2);
//					AccelerometerChart.joinFiles("D:\\_proyectoAlz\\analysis\\accelerometer\\counts\\" + userID + "\\", d1 + ".alz", d1 + ".alz5", d2 + ".alz5");
//				}

				for (int i = DataUtil.days1_5[userID - 1][0]; i <= 30; i++) {
					d1 = new SQLQuery().plotAccelerometerIntensity(userID, i + "/11/2011", 1);
					d2 = new SQLQuery().plotAccelerometerIntensity(userID, i + "/11/2011", 2);
					AccelerometerChart.joinFiles("D:\\_proyectoAlz\\analysis\\accelerometer\\counts\\" + userID + "\\", d1 + ".alz", d1 + ".alz5", d2 + ".alz5");
					AccelerometerChart.joinFiles("D:\\_proyectoAlz\\analysis\\accelerometer\\counts\\" + userID + "\\", d1 + ".ct", d1 + ".ct5", d2 + ".ct5");
				}
				for (int i = 1; i <= DataUtil.days1_5[userID - 1][1]; i++) {
					d1 = new SQLQuery().plotAccelerometerIntensity(userID, i + "/12/2011", 1);
					d2 = new SQLQuery().plotAccelerometerIntensity(userID, i + "/12/2011", 2);
					AccelerometerChart.joinFiles("D:\\_proyectoAlz\\analysis\\accelerometer\\counts\\" + userID + "\\", d1 + ".alz", d1 + ".alz5", d2 + ".alz5");
					AccelerometerChart.joinFiles("D:\\_proyectoAlz\\analysis\\accelerometer\\counts\\" + userID + "\\", d1 + ".ct", d1 + ".ct5", d2 + ".ct5");
				}

				new SQLQuery().plotAccelerometerIntensityFromFiles(userID, "D:\\_proyectoAlz\\analysis\\accelerometer\\counts\\" + userID + "\\");
			}

		} catch (Exception e) {
			e.printStackTrace();
		}*/


	    /*try {
			userID = 6;
			for (int i = 8; i <= 31; i++) {
				d1 = new SQLQuery().plotAccelerometerIntensity(userID, i + "/12/2011", 1);
				d2 = new SQLQuery().plotAccelerometerIntensity(userID, i + "/12/2011", 2);
				AccelerometerChart.joinFiles("D:\\_proyectoAlz\\analysis\\accelerometer\\counts\\" + userID + "\\", d1 + ".alz", d1 + ".alz5", d2 + ".alz5");
				AccelerometerChart.joinFiles("D:\\_proyectoAlz\\analysis\\accelerometer\\counts\\" + userID + "\\", d1 + ".ct", d1 + ".ct5", d2 + ".ct5");
			}

			for (int i = 1; i <= 3; i++) {
				d1 = new SQLQuery().plotAccelerometerIntensity(userID, i + "/1/2012", 1);
				d2 = new SQLQuery().plotAccelerometerIntensity(userID, i + "/1/2012", 2);
				AccelerometerChart.joinFiles("D:\\_proyectoAlz\\analysis\\accelerometer\\counts\\" + userID + "\\", d1 + ".alz", d1 + ".alz5", d2 + ".alz5");
				AccelerometerChart.joinFiles("D:\\_proyectoAlz\\analysis\\accelerometer\\counts\\" + userID + "\\", d1 + ".ct", d1 + ".ct5", d2 + ".ct5");
			}

			new SQLQuery().plotAccelerometerIntensityFromFiles(userID, "D:\\_proyectoAlz\\analysis\\accelerometer\\counts\\" + userID + "\\");

		} catch (Exception e) {
			e.printStackTrace();
		}

		try {
			for (userID = 7; userID <= 10; userID++) {

				for (int i = DataUtil.days7_10[userID - 7][0]; i <= DataUtil.days7_10[userID - 7][1]; i++) {
					d1 = new SQLQuery().plotAccelerometerIntensity(userID, i + "/12/2011", 1);
					d2 = new SQLQuery().plotAccelerometerIntensity(userID, i + "/12/2011", 2);
					AccelerometerChart.joinFiles("D:\\_proyectoAlz\\analysis\\accelerometer\\counts\\" + userID + "\\", d1 + ".alz", d1 + ".alz5", d2 + ".alz5");
					AccelerometerChart.joinFiles("D:\\_proyectoAlz\\analysis\\accelerometer\\counts\\" + userID + "\\", d1 + ".ct", d1 + ".ct5", d2 + ".ct5");
				}

				new SQLQuery().plotAccelerometerIntensityFromFiles(userID, "D:\\_proyectoAlz\\analysis\\accelerometer\\counts\\" + userID + "\\");
			}

		} catch (Exception e) {
			e.printStackTrace();
		}*/

/*		try {
			for (userID = 11; userID <= 15; userID++) {
				for (int i = DataUtil.daysLast5[userID - 11][0]; i <= 31; i++) {
					d1 = new SQLQuery().plotAccelerometerIntensity(userID, i + "/1/2012", 1);
					d2 = new SQLQuery().plotAccelerometerIntensity(userID, i + "/1/2012", 2);
					AccelerometerChart.joinFiles("D:\\_proyectoAlz\\analysis\\accelerometer\\counts\\" + userID + "\\", d1 + ".alz", d1 + ".alz5", d2 + ".alz5");
					AccelerometerChart.joinFiles("D:\\_proyectoAlz\\analysis\\accelerometer\\counts\\" + userID + "\\", d1 + ".ct", d1 + ".ct5", d2 + ".ct5");
				}
				for (int i = 1; i <= DataUtil.daysLast5[userID - 11][1]; i++) {
					d1 = new SQLQuery().plotAccelerometerIntensity(userID, i + "/2/2012", 1);
					d2 = new SQLQuery().plotAccelerometerIntensity(userID, i + "/2/2012", 2);
					AccelerometerChart.joinFiles("D:\\_proyectoAlz\\analysis\\accelerometer\\counts\\" + userID + "\\", d1 + ".alz", d1 + ".alz5", d2 + ".alz5");
					AccelerometerChart.joinFiles("D:\\_proyectoAlz\\analysis\\accelerometer\\counts\\" + userID + "\\", d1 + ".ct", d1 + ".ct5", d2 + ".ct5");
				}

				new SQLQuery().plotAccelerometerIntensityFromFiles(userID, "D:\\_proyectoAlz\\analysis\\accelerometer\\counts\\" + userID + "\\");
			}
		} catch (Exception e) {
			e.printStackTrace();
		}*/










//		for (userID = 1; userID <= 15; userID++) {
//			new SQLQuery().plotNFCSummaryData(userID);
//		}

//		new SQLQuery().plotAccelerometerAndNFCData(userID, day);
//		new SQLQuery().plotAccelerometerAndNFCSummaryData(userID, day);
//		new SQLQuery().plotAccelerometerSummaryData(userID);

//		new SQLQuery().plotAccelerometerAndNFCData(2, day);



//		userID = 15;
//		d1 = new SQLQuery().plotAccelerometerSummaryData(userID, "10/2/2012", 1);
//		d2 = new SQLQuery().plotAccelerometerSummaryData(userID, "10/2/2012", 2);
//		AccelerometerChart.joinFiles("D:\\_proyectoAlz\\analysis\\gps charts\\" + userID + "\\", d1 + ".alz", d1 + ".alz5", d2 + ".alz5");
//		new SQLQuery().plotAccelerometerSummaryDataFromFiles(13);

//
//		try {
//			userID = 7;
//			for (int i = 9; i <= 30; i++) {
//				d1 = new SQLQuery().plotAccelerometerSummaryData(userID, i + "/12/2011", 1);
//				d2 = new SQLQuery().plotAccelerometerSummaryData(userID, i + "/12/2011", 2);
//				AccelerometerChart.joinFiles("D:\\_proyectoAlz\\analysis\\gps charts\\" + userID + "\\", d1 + ".alz", d1 + ".alz5", d2 + ".alz5");
//			}
//
//			new SQLQuery().plotAccelerometerSummaryDataFromFiles(userID);
//
//
//			userID = 10;
//			for (int i = 7; i <= 27; i++) {
//				d1 = new SQLQuery().plotAccelerometerSummaryData(userID, i + "/12/2011", 1);
//				d2 = new SQLQuery().plotAccelerometerSummaryData(userID, i + "/12/2011", 2);
//				AccelerometerChart.joinFiles("D:\\_proyectoAlz\\analysis\\gps charts\\" + userID + "\\", d1 + ".alz", d1 + ".alz5", d2 + ".alz5");
//			}


//		new SQLQuery().plotAccelerometerSummaryDataFromFiles(3);
//		new SQLQuery().plotGPSData(9);
//		for (userID = 1; userID <= 15; userID++) {
//			new SQLQuery().plotAccelerometerSummaryDataFromFiles(userID);
//			new SQLQuery().plotGPSData(userID);
//		}
//		for (userID = 1; userID <= 15; userID++) {
//			new SQLQuery().plotAccelerometerSummaryDataFromFiles(userID);
//		}
//
//		} catch (Exception e) {
//			e.printStackTrace();
//		}
		
//		d1 = new SQLQuery().plotAccelerometerSummaryData(12, "8/2/2012", 1);
//		d2 = new SQLQuery().plotAccelerometerSummaryData(12, "8/2/2012", 2);
//		AccelerometerChart.joinFiles("D:\\_proyectoAlz\\analysis\\gps charts\\" + 3 + "\\", d1 + ".alz", d1 + ".alz5", d2 + ".alz5");
//		new SQLQuery().plotAccelerometerSummaryDataFromFiles(12);



//		new SQLQuery().plotNFCData(userID, day, true, false);

//		for (int i = 11; i < 16; i++) {
//			userID = i;
//			new SQLQuery().plotNFCData(userID, day, false, true);
//		}

//
//		userID = 1;
//		new SQLQuery().saveAccelerometerData();
//		new SQLQuery().displayGPSData(userID);
//		new SQLQuery().displayGPSData(userID, true, false);
//		new SQLQuery().displayGPSData(userID, day, true, false);


		/*for (userID = 1; userID <= 5; userID++) {
			System.out.println("User " + userID);
			
			int beginDay = Integer.valueOf(DataUtil.dates[userID - 1][0].substring(0, DataUtil.dates[userID - 1][0].indexOf("/")));
//			System.out.println(beginDay);
			for (int d = beginDay; d <= 31; d++) {
				new SQLQuery().createGPSClusterReport(userID, beginDay + "/11/2011", d + "/11/2011");
			}

			int endDay = Integer.valueOf(DataUtil.dates[userID - 1][1].substring(0, DataUtil.dates[userID - 1][1].indexOf("/")));
//			System.out.println("END " + endDay);
			for (int d = 1; d <= endDay; d++) {
				new SQLQuery().createGPSClusterReport(userID, beginDay + "/11/2011", d + "/12/2011");
			}
		}*/

		/*for (userID = 6; userID <= 10; userID++) {
			System.out.println("User " + userID);

			int beginDay = Integer.valueOf(DataUtil.dates[userID - 1][0].substring(0, DataUtil.dates[userID - 1][0].indexOf("/")));
			int endDay = Integer.valueOf(DataUtil.dates[userID - 1][1].substring(0, DataUtil.dates[userID - 1][1].indexOf("/")));
			if (userID == 6) {
				endDay = 31;
			}
			for (int d = beginDay; d <= endDay; d++) {
				new SQLQuery().createGPSClusterReport(userID, beginDay + "/12/2011", d + "/12/2011");
			}
			
			if (userID == 6) {
				for (int d = 1; d <= 3; d++) {
					new SQLQuery().createGPSClusterReport(userID, beginDay + "/12/2011", d + "/1/2012");
				}

			}

		}*/


		/*for (userID = 11; userID <= 15; userID++) {
			System.out.println("User " + userID);

			int beginDay = Integer.valueOf(DataUtil.dates[userID - 1][0].substring(0, DataUtil.dates[userID - 1][0].indexOf("/")));
			for (int d = beginDay; d <= 31; d++) {
				new SQLQuery().createGPSClusterReport(userID, beginDay + "/1/2012", d + "/1/2012");
			}

			int endDay = Integer.valueOf(DataUtil.dates[userID - 1][1].substring(0, DataUtil.dates[userID - 1][1].indexOf("/")));
			for (int d = 1; d <= endDay; d++) {
				new SQLQuery().createGPSClusterReport(userID, beginDay + "/1/2012", d + "/2/2012");
			}
		}*/



/*		for (int user = 1; user < 16; user++) {
			new SQLQuery().plotGPSData(user);
		}*/
//		new SQLQuery().plotGPSData(userID, day);

		/*int begin = 4;
		int end = 8;
		for (; begin <= end; begin++) {
			day = begin + "/1/2012";
			new SQLQuery().createWifiDataFile(userID, day);
		}*/

	}

	// Release resources
	public void close(Connection con, Statement stmt, ResultSet rs) {
        if (rs != null) {
                try {
                        rs.close();
                } catch (SQLException e) {
                }
        }
        if (stmt != null) {
                try {
                        stmt.close();
                } catch (SQLException e) {
                }
        }
        if (con != null) {
                try {
                        con.close();
                } catch (SQLException e) {
                }
        }
	}

}
